<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class UsersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	 public $components = array('Paginator', 'Session');
         
         
         //If we are not allowed          
         public function beforeFilter() {
            parent::beforeFilter();
             $this->Auth->allow('add'); // We can remove this line after we're finished
        }
         

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
                debug($this->User->find('first', $options));
		$this->set('user', $this->User->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->User->create();                        
			if ($this->User->save($this->request->data)) {       
                            
				$this->Session->setFlash(__('The user has been saved.'));                                                                
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$groups = $this->User->Group->find('list');
		$departments = $this->User->Department->find('list');
		$this->set(compact('groups', 'departments'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->User->exists($id)) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash(__('The user has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
			$this->request->data = $this->User->find('first', $options);
		}
		$groups = $this->User->Group->find('list');
		$departments = $this->User->Department->find('list');
		$this->set(compact('groups', 'departments'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->User->delete()) {
			$this->Session->setFlash(__('The user has been deleted.'));
		} else {
			$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
        

        public function login() {
            if ($this->request->is('post')) {
                if ($this->Auth->login()) {                    
                    //check admin rights.
                    $this->Session->write('is_admin', false);
                    if ($this->User->bestGroup() == 7){
                        $this->Session->write('is_admin', true);
                    }                    
                    
                    return $this->redirect($this->Auth->redirect());
                 }
                $this->Session->setFlash(__('Invalid username or password, try again'), 'alert', array(	'plugin' => 'BoostCake',
                    'class' => 'alert-danger'), 'auth');
            }
        }

        

        public function logout() {
            $this->Session->write('is_admin', false);
            return $this->redirect($this->Auth->logout());
        }        
                        
}
